//
// Created by firear on 21-1-6.
//

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "ALog.h"


extern char _binary_nconverter_jar_start;
extern char _binary_nconverter_jar_end;
extern char _binary_nconverter_jar_size;
#define TARGET_JAR_PATH "/data/local/tmp/nconverter.jar"


void releasejar()
{
    if(access(TARGET_JAR_PATH, F_OK)){
        LOGD("release jar to " TARGET_JAR_PATH "\n");
        FILE *fp = fopen(TARGET_JAR_PATH, "w");
        if(fp){
            fwrite(&_binary_nconverter_jar_start, (size_t)&_binary_nconverter_jar_size, 1, fp);
            fclose(fp);
        }else{
            LOGD("release jar failed %s\n", strerror(errno));
        }
    }
}

void printusage()
{
    printf("[options] inputfilepath outputfilepath \n"
           "    default parm:1080P hevc  4000000  192000\n"
           "--list  \n"
           "    -l  <e/d>\n"
           "--dumpfile  \n"
           "    -d <filepath>\n"
           "--videoresolution  \n"
           "    -r <720/1080/2160>\n"
           "--videocodec  \n"
           "    -c <avc/hevc>\n"
           "--videobitrate  \n"
           "    -B <8000000/6000000/4000000...>\n"
           "--audiobitrate  \n"
           "    -b <192000/...>\n");
}

int main(int argc, char *argv[])
{
    //*    [ld/le/df] [1.mp4] [2.mp4] [720/1080/2160] [avc/hevc] [vbitrate] [abitreate]
    releasejar();
    char * execargs[12] = {
            "app_process",
            "-Djava.class.path=" TARGET_JAR_PATH,
            "/data/local/tmp/",
            "com.nconverter.NConverter",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            NULL,
    };

    static const struct option long_options[] = {
        {"list",                required_argument, NULL, 'l'},
        {"dumpfile",            required_argument, NULL, 'd'},
        {"videoresolution",     required_argument, NULL, 'r'},
        {"videocodec",          required_argument, NULL, 'c'},
        {"videobitrate",        required_argument, NULL, 'B'},
        {"audiobitrate",        required_argument, NULL, 'b'},
        {"help",                no_argument,        NULL, 'h'},
    };

    optind = 0; // reset to start from the first argument in tests
    char **jargs = execargs+4;
    int c;
    while ((c = getopt_long(argc, argv, "l:r:c:B:b:d:h",
                            long_options, NULL)) != -1) {
        switch (c) {
            case 'l':
                if(!strcmp(optarg, "e")){
                    jargs[0] = "le";
                }else{
                    jargs[0] = "ld";
                }
                break;
            case 'd':
                jargs[0] = "df";
                jargs[1] = optarg;
                break;
            case 'r':
                LOGD("r:%s", optarg);
                jargs[3] = optarg;
                break;
            case 'c':
                LOGD("c:%s", optarg);
                jargs[4] = optarg;
                break;
            case 'B':
                LOGD("B:%s", optarg);
                jargs[5] = optarg;
                break;
            case 'b':
                LOGD("b:%s", optarg);
                jargs[6] = optarg;
                break;
            case 'h':
            default: /* '?' */
                printusage();
                exit(EXIT_FAILURE);
        }
    }

    size_t fileindex = 1;
    while (optind < argc && fileindex < 3){
        jargs[fileindex++] = argv[optind++];
    }

#ifndef NDEBUG
    for(size_t i = 0; i < sizeof(execargs)/sizeof(execargs[0]); i++){
        LOGD("argument = %zd %s", i, execargs[i]);
    }
#endif
    int ret = 0;
    ret = execvp("app_process", execargs);
    if(ret){
        LOGD("%s", strerror(errno));
    }
    return ret;
}
